﻿using cfg.Myskill;
using UnityEngine;

public class PlayerBackHoleState: PlayerStateBase
{
    public PlayerBackHoleState(ControllerBase controller, StateMachineBase stateMachine, string stateName, string animatorParameter) 
        : base(controller, stateMachine, stateName, animatorParameter)
    {
    }

    private BlackHoleSkill skill;
    private BlackHole skillInfo;
    private bool hasTriggerCreate;
    public override void Enter()
    {
        base.Enter();
        SelfController.mRigidbody2D.AddForce(new Vector2(0, Setting.jumpForce), ForceMode2D.Impulse);
        if (skill==null)
        {
            skill = SkillManager.Instance.GetSkill<BlackHoleSkill>(Setting.curHoleSkill);
            skillInfo = LoadCofigManager.Instance.GetSkillInfo<BlackHole>(Setting.curHoleSkill);
        }
        hasTriggerCreate = false;
        
    }

    public override void Exit()
    {
        base.Exit();
        SelfController.mRigidbody2D.gravityScale = 3.5f;
        SelfController.mCollider2D.enabled = true;
        SelfController.mRigidbody2D.bodyType = RigidbodyType2D.Dynamic;
        SelfController.mRigidbody2D.constraints = RigidbodyConstraints2D.FreezeRotation;
    }

    public override void Update()
    {
        base.Update();
            
        if (controller.GetVelocity().y < 0&&!hasTriggerCreate)
        {
            hasTriggerCreate  = true;
            //取消重力 关闭碰撞检测
            SelfController.mRigidbody2D.gravityScale = 0;
            SelfController.mCollider2D.enabled = false;
            SelfController.mRigidbody2D.bodyType = RigidbodyType2D.Kinematic;
            SelfController.mRigidbody2D.constraints = RigidbodyConstraints2D.FreezeAll;
            
            SkillManager.Instance.CreateClone<BlackHoleSkill>
                ("BlackHole",controller.transform.position,controller.faceDir,Setting.curHoleSkill);
            return;
        }
        
        if (stateDuration>skillInfo.SkillDuration)
        {
            stateMachine.ChangeState(Setting.air);
            //通知技能controller回收黑洞
            EventCenter.Instance.EventTrigger(E_EventType.Hole_Call_Back);
        }
    }
}